package com.hxkj.service.http.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hxkj.client.service.TemplteServiceClient;
import com.hxkj.client.service.http.VisualizationServiceClient;
import com.hxkj.domain.po.FreeMarker;
import com.hxkj.domain.po.Templet;
import com.hxkj.domain.po.VisualBean;
import com.hxkj.service.http.VisualizationService;
import com.hxkj.service.taglib.AnakyzeFreeMakerShowService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 可视化实现类
 * <p>
 * Created by yaosy on 2018年8月16日13:44:02
 */
@Service
@Slf4j
public class VisualizationServiceImpl implements VisualizationService {

    @Autowired
    private AnakyzeFreeMakerShowService anakyzeFreeMakerShowService;

    @Autowired
    private VisualizationServiceClient visualizationServiceClient;

    @Autowired
    private TemplteServiceClient templetServiceClient;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CMS_UUID_START="<!--uuid";

    private static final String CMS_UUID_END="<!--_HXKJTAGS>";


    @Override
    public void resolve(VisualBean visualBean, HttpServletRequest request, HttpServletResponse response) throws Exception{
        try {
            String templteId = request.getParameter("templteId")==null?"":request.getParameter("templteId");
            String siteId = request.getParameter("siteId")==null?"":request.getParameter("siteId");
            String status = request.getParameter("status")==null?"":request.getParameter("status");

            // 清除本站点下所有的自定义标签的redis缓存
            redisTemplate.delete(redisTemplate.keys("directive_" + siteId + "*"));
            /**********get请求转post请求处理***************************************************************/
            visualBean.setSiteId(Long.valueOf(siteId));
            visualBean.setTemplteId(Long.valueOf(templteId));
            visualBean.setStatus(Long.valueOf(status));
            check(visualBean);
            /*********************************************************************************************/

            FreeMarker freeMarker = new FreeMarker();
            freeMarker.setTempletId(Long.valueOf(templteId));
            freeMarker.setSiteId(Long.valueOf(siteId));
            freeMarker.setStatus(Long.valueOf(status));
            freeMarker.setNote("1");//有值代表可视化;空值代表静态化生成
            JSONObject jsonObject =  anakyzeFreeMakerShowService.anakyzeFreeMakerShow(freeMarker);
            String content  = (String) jsonObject.get("filePath");
            Templet templet = new Templet();
            templet.setSiteId(freeMarker.getSiteId());
            templet.setDelTag((byte)0);
            templet.setTempletId(Long.valueOf(templteId));
            ResultDataStruct resultDataStruct =  templetServiceClient.get(templet);
            if(200!=resultDataStruct.getCode()){
                throw new CustomMessageException(resultDataStruct.getCode(),resultDataStruct.getMessage());
            }
            String templetString = JSON.toJSONString(resultDataStruct.getData());
            Templet t = (Templet) JSON.parseObject(templetString, new TypeReference<Templet>(){});
            if(t != null){
                response.getWriter().write(content+"\n"+CMS_UUID_START+":"+t.getTempletAlias()+ "-->");
            }else{
                throw new CustomMessageException(402,"模板不存在!");
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }


    public void replace(VisualBean visualBean)throws Exception{
        try{
            check(visualBean);
            ResultMessageStruct resultMessageStruct = visualizationServiceClient.replace(visualBean);
            if(resultMessageStruct.getCode() != 200){
                throw new CustomMessageException(402,resultMessageStruct.getMessage());
            }
        }catch (CustomMessageException e){
            throw e;
        }catch (Exception e){
            log.error("可视化解析错误",e);
            throw new CustomMessageException(401,"可视化解析错误");
        }
    }


    protected void check(VisualBean visualBean)throws Exception{
        if (visualBean.getSiteId() == null) {
            throw new CustomMessageException(402,"站点id为空!");
        }
        if(visualBean.getTemplteId()==null){
            throw new CustomMessageException(403,"模板id为空!");
        }
    }
}
